<?
	class User extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->library('email');
			$this->load->model('UserModel');
			$this->load->library('session');
			$this->load->helper('string');
		}
		protected function getIsPost()
		{
			return $_SERVER['REQUEST_METHOD'] == 'POST';
		}
		private final function showView($fileName, $data)
		{
			$l = $this->load;
			$l->view('Templates/header.php', $data);
			$l->view('Users/' . $fileName . '.php', $data);
			$l->view('Templates/footer.php', $data);
		}
		private final function getPostData()
		{
			$i = $this->input;
			$b = $this->UserModel;
			//$b->Id = $i->post('id');
			$b->FullName = $i->post('fullName');
			$b->Username = $i->post('username');
			$b->Email = $i->post('email');
			$b->Mobile = $i->post('mobile');
			$b->Landline = $i->post('landline');
			return $b;
		}
		public final function create()
		{
			$data['title'] = 'Add a user';
			$data['user'] = $this->UserModel->addUser($this->getPostData());
			$this->showView('create', $data);
		}
		public final function read($id = null)
		{
			if($id == null)
			{
				$data['title'] = 'View all users';
				$data['users'] = $this->UserModel->getAllUsers();
			}
			else
			{
				$data['title'] = 'View user details';
				$data['users'] = $this->UserModel->getUserById($id);
			}
			$this->showView('read', $data);
		}
		public final function update()
		{
			$data['title'] = 'Update a user';
			$data['user'] = $this->UserModel->updateUser($this->getPostData());
			$this->showView('update', $data);
		}
		public final function delete()
		{
			$id = $this->input->post('id');
			$data['title'] = 'Remove a user';
			$data['user'] = $this->UserModel->removeUserById($id);
			$this->showView('delete', $data);
		}
		public final function resetPassword($userId, $token)
		{
			$p = $this->input->post;
			if($this->getIsPost())
			{
				$this->UserModel->resetPassword();
			}
			else
			{
				$data['title'] = 'Reset your password';
				$data['validToken'] = $this->UserModel->getIsValidToken($userId, $token);
				$this->showView('User/password', $data);
			}
		}
		public final function sendRequestResetPassword()
		{
			$p = $this->input->post;
			if($this->getIsPost())
			{
				$u = $this->UserModel->getUserByUsername($p('username'));
				$this->UserModel->updateResetPasswordFlag($p('username'), true);
				$p = 'User/resetPassword' . $u->Id . '/' . random_string('alnum', 16); 
				$msg = 'Hi ' . $u->FullName . ',<br /><br />' . 
						'You have requested to reset your password.<br />' . 
						'Click <a href="'. $p . 
						'">here</a>to request for a new password.';
				$this->sendEmail
				(
					'noreply@digital-lifeline.com', 
					$p('username'),
					'Password Reset',
					$msg
				);
			}
		}
		public final function updatePassword()
		{
			$p = $this->input->post;
			$this->UserModel->updatePassword($p('id'), $p('newPassword'));
		}
		public final function sendEmail($from, 
										$to,
										$subject,
										$message,
										$cc = NULL, 
										$bcc = NULL)
		{
			$e = $this->email;
			$e->from($from);
			$e->to($to); 
			if($cc != NULL) $e->cc($carbonCopies); 
			if($bcc != NULL) $e->bcc($blindCarbonCopies);
			$e->subject($subject);
			$e->message($message);
			$e->send();
		}
	}